package lib.self.ex.activity;

import android.os.Bundle;
import androidx.annotation.FloatRange;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;

import lib.self.R;
import lib.self.util.res.ResLoader;
import lib.self.utils.UiUtil;


/**
 * description: 对话框式的activity
 * author: Steven Xiao
 * date: 2017/5/12
 */
public abstract class DialogActivityEx extends ActivityEx {

    private OnClickListener mFinishListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.DialogActivityStyle);
        super.onCreate(savedInstanceState);

        setAttrs();

//        setCanceledOnTouchOutside();

        setDimAmount(getDimAmount());
    }

    protected float getDimAmount() {
        return 0.5f;
    }

    /**
     * 设置背景色 弹出动画 是否全屏等
     */
    protected void setAttrs() {
        setBackgroundColor(ResLoader.getColor(R.color.transparent));
        UiUtil.setWindowToFullScreen(this);
    }

    @Override
    public void initTitleBar() {
    }

    @Override
    public void setViewsValue() {
    }

    @Override
    protected void startInAnim() {
        startActSwitch(R.anim.no_effct, R.anim.no_effct);
    }

    @Override
    protected void startOutAnim() {
        startActSwitch(R.anim.no_effct, R.anim.no_effct);
    }

    protected void setFinishClicker(View view) {
        if (view == null) {
            return;
        }

        if (mFinishListener == null) {
            mFinishListener = v -> finish();
        }
        view.setOnClickListener(mFinishListener);
    }

    protected void setFinishListener(@IdRes int viewId) {
        if (mFinishListener == null) {
            mFinishListener = v -> finish();
        }

        View view = findViewById(viewId);
        if (view != null) {
            view.setOnClickListener(mFinishListener);
        }
    }

    protected void setCanceledOnTouchOutside() {
        setFinishClicker(getDecorView());
    }

    /**
     * 设置空白处黑暗度
     *
     * @param amount 0-1.0 0全透明 1全黑
     */
    protected void setDimAmount(@FloatRange(from = 0, to = 1.0f) float amount) {
        LayoutParams params = getWindow().getAttributes();
        params.dimAmount = amount;
        getWindow().setAttributes(params);
        getWindow().addFlags(LayoutParams.FLAG_DIM_BEHIND);
    }
}
